package SyntheticData;

/**
 *
 */
public class Vertex {
    private int index;
    private int[] elements;
    private int label;

    Vertex(int index, int numElements)
    {
        this.index = index;
        this.elements = new int[numElements];
    }

    Vertex(int index, Vertex other)
    {
        this.index = index;
        this.elements = other.elements.clone();
    }

    public int getIndex() {
        return index;
    }
    
    public int getNumElements() {
        return elements.length;
    }

    public int getElement(int i) {
        return elements[i];
    }

    public int getLabel() {
        return label;
    }

    public void randomizeElements(int numLabels) {
        for (int i = 0; i < elements.length; i++) {
            elements[i] = (int) (Math.random() * numLabels);
        }
    }

    public void randomizeElements(int numLabels, double biasedProb) {
        int l = (int) (Math.random() * numLabels);
        for (int i = 0; i < elements.length; i++) {
            if (Math.random() <= biasedProb) {
                elements[i] = l;
            } else {
            	//???ESER
                int e = (int) (Math.random() * (numLabels - 1));
                if (e < l) {
                    elements[i] = e;
                } else {
                    elements[i] = e + 1;
                }
            }
        }
    }

    public void mutateElements(int numLabels, double reassignProb) {
        for (int i = 0; i < elements.length; i++) {
            if (Math.random() < reassignProb) {
                elements[i] = (int) (Math.random() * numLabels);
            }
        }
    }

    public void determineLabel(int numLabels) {
        int[] occurrences = new int[numLabels];
        int maxOccurrences = 0;
        for (int e: elements) {
            occurrences[e]++;
            if (occurrences[e] > maxOccurrences) {
                maxOccurrences = occurrences[e];
            }
        }

        int[] candidates = new int[numLabels];
        int numCandidates = 0;
        for (int l = 0; l < numLabels; l++) {
            if (occurrences[l] == maxOccurrences) {
                candidates[numCandidates++] = l;
            }
        }

        int l = (int) (Math.random() * numCandidates);
        label = candidates[l];
    }
}
